package com.ebeitech.doorapp.view.customviews;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.ebeitech.doorapp.R;
import com.ebeitech.library.util.StringUtil;
import com.ebeitech.library.util.ViewUtil;

public class RoundPercentTextView extends TextView {

	// 圆形边框的厚度
	private int mBorderThickness = 0;
	private Context mContext;
	private int defaultColor = 0xFFFF0000;
	// 圆形边框的颜色
	private int mBorderColor = 0;
	// 外围框的半径差
    private int mOutsideRadiusDelta = 0; 
	// 控件默认长、宽
	private int defaultWidth = 0;
	private int defaultHeight = 0;
	
	private int radius;
	private Float mRate = 0.0f;
	private String mTitle;

	public RoundPercentTextView(Context context) {
		super(context);
		mContext = context;
		init(mContext);
	}
	
	public RoundPercentTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		setCustomAttributes(attrs);
		init(mContext);
	}

	public RoundPercentTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		setCustomAttributes(attrs);
		init(mContext);
	}
	
	private void setCustomAttributes(AttributeSet attrs) {
		TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.RoundPercentImage);
		mBorderThickness = a.getDimensionPixelSize(R.styleable.RoundPercentImage_border_thickness, ViewUtil.dp2px(mContext, 2));
		mBorderColor = a.getColor(R.styleable.RoundPercentImage_border_color, defaultColor);
	}

	private void init(Context context) {
		// TODO Auto-generated method stub
		if (mOutsideRadiusDelta == 0) {
			mOutsideRadiusDelta = ViewUtil.dp2px(mContext, 2);
		}
		ViewTreeObserver vto = getViewTreeObserver();
		vto.addOnGlobalLayoutListener(mGlobalLayoutListener);
	}

	@Override
	public void setVisibility(int visibility) {
		// TODO Auto-generated method stub
		super.setVisibility(visibility);
		if (visibility == View.VISIBLE) {
			if (defaultWidth == 0) {
				ViewTreeObserver vto = getViewTreeObserver();
				vto.addOnGlobalLayoutListener(mGlobalLayoutListener);
			}
		} else {
			getViewTreeObserver().removeGlobalOnLayoutListener(mGlobalLayoutListener);
		}
	}

	private ViewTreeObserver.OnGlobalLayoutListener mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
		@Override
		public void onGlobalLayout() {
			int width = getMeasuredWidth();
			if (width != 0) {
				getViewTreeObserver().removeGlobalOnLayoutListener(this);
			}
			if (defaultWidth == 0 && width != 0) {
				defaultWidth = width;
				defaultHeight = getMeasuredHeight();
				invalidate();
			}
		}
	};

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		Paint paint = getCroppedRoundPaint();
		paint.setColor(getResources().getColor(R.color.ebei_divider_gray));
		paint.setStrokeWidth(mBorderThickness + mOutsideRadiusDelta * 2);
		canvas.drawCircle(defaultWidth / 2, defaultWidth / 2, defaultWidth / 2 - mBorderThickness / 2 - mOutsideRadiusDelta, paint);
		drawCircleBorder(canvas);
	}
	
	/**
	 * 获取画笔
	 * @return
	 */
	public Paint getCroppedRoundPaint() {
		Paint paint = new Paint();
		paint.setColor(getResources().getColor(R.color.ebei_blue));
		paint.setStyle(Style.STROKE);
		// 去锯齿 
		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		paint.setDither(true);
		paint.setPathEffect(null);
		// 设置paint的外框宽度 
		paint.setStrokeWidth(mBorderThickness);
		return paint;
	}
	
	/**
	 * 边缘画圆
	 */
	private void drawCircleBorder(Canvas canvas) {
		Paint paint = getCroppedRoundPaint();
		radius = defaultWidth / 2 - mBorderThickness / 2 - mOutsideRadiusDelta;
		// 边缘画圈
		if (mRate >= 0 && mRate <= 100) {
			RectF oval = new RectF(defaultWidth / 2 - radius, defaultHeight / 2 - radius,
					defaultWidth / 2 + radius, defaultHeight / 2 + radius);
			int rotation = (int) (mRate * 360 / 100);
			canvas.drawArc(oval, 270, rotation, false, paint);
		}
		
		// 画文字
		paint.setTextSize(getResources().getDimension(R.dimen.ebei_common_text_size));
		paint.setStrokeWidth(1);
		paint.setStyle(Style.FILL);
		paint.setColor(getResources().getColor(R.color.ebei_text_common));
		int width = radius - ViewUtil.dp2px(mContext, 10);
		int height = (int) ViewUtil.getTextHeight(paint) + 10;
		RectF rect = new RectF((defaultWidth - width) / 2, defaultHeight / 2 - height,
				(width + defaultWidth) / 2, defaultHeight / 2);
		if(!StringUtil.isStringNullOrEmpty(mTitle)) {
			Point point = ViewUtil.getCenterTextPonit(rect, paint, mTitle);
			canvas.drawText(mTitle, point.x, point.y, paint);
		}

		if (mRate > -1 && mRate < 101) {
			int marginTop = ViewUtil.dp2px(mContext, 5);
			paint.setColor(getResources().getColor(R.color.ebei_blue));
			paint.setTextSize(getResources().getDimension(R.dimen.ebei_common_small_text_size));
			height = (int) ViewUtil.getTextHeight(paint) + marginTop;
			if (StringUtil.isStringNullOrEmpty(mTitle)) {
				rect = new RectF((defaultWidth - width) / 2, (defaultHeight - height) / 2,
						(width + defaultWidth) / 2, (defaultHeight + height) / 2);
				Point point = ViewUtil.getCenterTextPonit(rect, paint, mRate + "%");
				canvas.drawText(mRate + "%", point.x, point.y, paint);
			} else {
				rect = new RectF((defaultWidth - width) / 2, defaultHeight / 2 + marginTop,
						(width + defaultWidth) / 2, defaultHeight / 2 + height + marginTop);
				Point point = ViewUtil.getCenterTextPonit(rect, paint, mRate + "%");
				canvas.drawText(mRate + "%", point.x, point.y, paint);
			}
		}
	}
	
	public void setRate(Float rate) {
		this.mRate = rate;
		invalidate();
	}
	public void setTitle(String title) {
		this.mTitle = title;
		invalidate();
	}
}
